package com.es.common;

import java.sql.ResultSet;
import java.util.ArrayList;

import com.es.util.db.Dao;

public class CommonBiz {
	
	public ArrayList<CommonBean> getCommonByType(String type)
	throws Exception{
		Dao dao = new Dao();
		ArrayList<String> sqlParam = new ArrayList<String>();
		ArrayList<CommonBean> commonBeans = new ArrayList<CommonBean>();
		sqlParam.add(type);
		try{
			String sql = "select common_id, common_code, common_value from hrm_common where common_type = ?";
			ResultSet rs = dao.Select(sql, sqlParam);
			while(rs.next()){
				CommonBean cmnBean = new CommonBean();
				cmnBean.setCommonId(rs.getString("common_id"));
				cmnBean.setCommonCode(rs.getString("common_code"));
				cmnBean.setCommonValue(rs.getString("common_value"));
				cmnBean.setCommonType(type);
				commonBeans.add(cmnBean);
			}
		}catch(Exception ex){
			throw ex;
		}finally{
			dao.close();
		}
		return commonBeans;
	}
	
	public void deleteCommonById(String id) throws Exception{
		Dao dao = new Dao();
		ArrayList<String> sqlParam = new ArrayList<String>();
		sqlParam.add(id);
		String sql = "delete from hrm_common where common_id = ?";
		try{
			dao.update(sql, sqlParam);
		}catch(Exception ex){
			throw ex;
		}finally{
			dao.close();
		}
	}
	
	public CommonBean getCommonById(String id)throws Exception{
		Dao dao = new Dao();
		ArrayList<String> sqlParam = new ArrayList<String>();
		sqlParam.add(id);
		CommonBean cb = null;
		String sql = "select common_type, common_code, common_value from hrm_common where common_id = ?";
		try{
			ResultSet rs = dao.Select(sql, sqlParam);
			while(rs.next()){
				cb = new CommonBean();
				cb.setCommonId(id);
				cb.setCommonType(rs.getString("common_type"));
				cb.setCommonCode(rs.getString("common_code"));
				cb.setCommonValue(rs.getString("common_value"));
			}
		}catch(Exception ex){
			throw ex;
		}finally{
			dao.close();
		}
		return cb;
	}
	
	public CommonBean getCommonByTypeAndCode(String type, String code)throws Exception{
		Dao dao = new Dao();
		ArrayList<String> sqlParam = new ArrayList<String>();
		sqlParam.add(type);
		sqlParam.add(code);
		CommonBean cb = null;
		String sql = "select common_id, common_value from hrm_common where common_type = ? and common_code =?";
		try{
			ResultSet rs = dao.Select(sql, sqlParam);
			while(rs.next()){
				cb = new CommonBean();
				cb.setCommonId(rs.getString(1));
				cb.setCommonType(type);
				cb.setCommonCode(code);
				cb.setCommonValue(rs.getString("common_value"));
			}
		}catch(Exception ex){
			throw ex;
		}finally{
			dao.close();
		}
		return cb;
	}
	
	public boolean modifyCommon(String code, String value, String id) throws Exception{
		Dao dao = new Dao();
		boolean result = false;
		String sql = "update hrm_common set common_code=? , common_value = ? where common_id=?";
		ArrayList<String> sqlParam = new ArrayList<String>();
		sqlParam.add(code);
		sqlParam.add(value);
		sqlParam.add(id);		
		try{
			dao.update(sql, sqlParam);
			result = true;
		}catch(Exception ex){
			throw ex;
		}finally{
			dao.close();
		}
		return result;
	}
	
	public boolean insertCommon(String code, String value, String type) throws Exception{
		Dao dao = new Dao();
		boolean result = false;
		String sql = "insert into hrm_common (common_code, common_value, common_type) values(?,?,?)";
		ArrayList<String> sqlParam = new ArrayList<String>();
		sqlParam.add(code);
		sqlParam.add(value);
		sqlParam.add(type);		
		try{
			dao.update(sql, sqlParam);
			result = true;
		}catch(Exception ex){
			throw ex;
		}finally{
			dao.close();
		}
		return result;
	}

	public CommonBean getCommonByTypeAndValue(String type,
			String value)  throws Exception{
		// TODO Auto-generated method stub
		Dao dao = new Dao();
		ArrayList<String> sqlParam = new ArrayList<String>();
		sqlParam.add(type);
		sqlParam.add(value);
		CommonBean cb = null;
		String sql = "select common_id, common_code from hrm_common where common_type = ? and common_value =?";
		try{
			ResultSet rs = dao.Select(sql, sqlParam);
			while(rs.next()){
				cb = new CommonBean();
				cb.setCommonId(rs.getString(1));
				cb.setCommonType(type);
				cb.setCommonValue(value);
				cb.setCommonCode(rs.getString("common_code"));
			}
		}catch(Exception ex){
			throw ex;
		}finally{
			dao.close();
		}
		return cb;
	}
}
